package bsr.basara.autodata.controller;

import bsr.basara.autodata.constant.PowerConstants;
import bsr.basara.autodata.entity.ColumnInfo;
import bsr.basara.autodata.entity.TableInfo;
import bsr.basara.autodata.service.ColumnInfoService;
import bsr.basara.autodata.service.TableInfoService;
import bsr.basara.autodata.service.impl.AutoDataService;
import bsr.basara.autodata.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author basara
 */
@RestController
@RequestMapping("/table")
public class TableInfoController {

    @Autowired
    private TableInfoService tableInfoService;

    @Autowired
    private ColumnInfoService columnInfoService;

    @Resource
    private AutoDataService autoDataService;

    /**
     * 表信息列表
     */
    @RequestMapping("/all")
    public Result<?> all() {
        Iterable<TableInfo> all = tableInfoService.findAll();
        return Result.ok(all);
    }

    /**
     * 列信息列表
     */
    @RequestMapping("/columns")
    public Result<?> columns(Long tableId) {
        Iterable<ColumnInfo> columns = columnInfoService.findByTableId(tableId);
        return Result.ok(columns);
    }

    /**
     * 加载表列信息
     */
    @RequestMapping("/auto")
    public Result<?> auto() {
        autoDataService.autoTableAndCol(PowerConstants.INSPECTION_DEV_TABLES, PowerConstants.INSPECTION_HIS_TABLES);
        return Result.ok();
    }

}
